﻿package mortal.game.view.common.cd
{
    import flash.utils.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.cd.effect.*;
    import mortal.mvc.core.*;

    public class CDDataPublicCD extends CDData
    {
        private var _dic:Dictionary;
        private var _curList:Array;

        public function CDDataPublicCD()
        {
            this._dic = new Dictionary();
            return;
        }// end function

        public function addPublicCD(param1:Object, param2:ICDData) : void
        {
            if (this._dic[param1] != null)
            {
                return;
            }
            this._dic[param1] = param2;
            return;
        }// end function

        public function removePublicCD(param1:Object) : void
        {
            if (this._dic[param1] == null)
            {
                return;
            }
            delete this._dic[param1];
            return;
        }// end function

        override public function startCoolDown() : void
        {
            this.resetCDDatas();
            super.startCoolDown();
            return;
        }// end function

        override protected function callPercentage(param1:int) : void
        {
            var _loc_2:ICDData = null;
            _percentage = Math.ceil(param1 / totalTime * 100);
            if (_percentage != _lastPercentage)
            {
                _lastPercentage = _percentage;
                for each (_loc_2 in this._curList)
                {
                    
                    _loc_2.caller.call(CDEffectTimerType.Percentage, _percentage);
                }
            }
            return;
        }// end function

        override protected function callFrame(param1:int) : void
        {
            var _loc_2:ICDData = null;
            var _loc_4:* = _lastFrame + 1;
            _lastFrame = _loc_4;
            for each (_loc_2 in this._curList)
            {
                
                _loc_2.caller.call(CDEffectTimerType.Frame, _lastFrame);
            }
            return;
        }// end function

        override protected function callSecond(param1:int) : void
        {
            var _loc_2:ICDData = null;
            _second = Math.ceil((totalTime - param1) / 1000);
            if (_second != _lastSecond)
            {
                _lastSecond = _second;
                for each (_loc_2 in this._curList)
                {
                    
                    _loc_2.caller.call(CDEffectTimerType.Second, _second);
                }
            }
            return;
        }// end function

        override protected function setViewToEnd() : void
        {
            var _loc_1:ICDData = null;
            for each (_loc_1 in this._curList)
            {
                
                if (_loc_1.caller)
                {
                    _loc_1.caller.call(CDEffectTimerType.Percentage, 101);
                    _loc_1.caller.call(CDEffectTimerType.Second, 0);
                    _loc_1.caller.call(CDEffectTimerType.Frame, 0);
                    _loc_1.caller.call(CDEffectTimerType.FinishedCallback);
                }
                _loc_1.isCoolDown = false;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CDPublicCDEnd));
            return;
        }// end function

        private function resetCDDatas() : void
        {
            var _loc_1:ICDData = null;
            this._curList = [];
            for each (_loc_1 in this._dic)
            {
                
                if (!_loc_1.isCoolDown)
                {
                    this._curList.push(_loc_1);
                    _loc_1.isCoolDown = true;
                    continue;
                }
                if (_loc_1.leftTime < _totalTime)
                {
                    _loc_1.stopCoolDown();
                    _loc_1.isCoolDown = true;
                    this._curList.push(_loc_1);
                    continue;
                }
            }
            return;
        }// end function

    }
}
